/**
 * Create in 2012-4-25.
 */
package kit4j.tests.jdk.util;

import static org.junit.Assert.assertEquals;

import java.util.Calendar;

import kit4j.util.lang.CalendarUtil;

import org.junit.Test;

/**
 * 
 * @author MiniKnife
 * 
 */
public class TestDate {
	
	/**
	 * 测试实现从1970年1月1日到某个时间经过的毫秒数计算出该时间的年份、月份、日份、小时、分钟、秒钟。
	 */
	@Test
	public void testCalculateDateTimeFromMilliSecond() {
		Calendar cal = Calendar.getInstance();
		int timeZone = 8;
		long ms = cal.getTimeInMillis();
		long seconds = ms / 1000;
		long minutes = seconds / 60;
		long hours = minutes / 60;
		long days = hours / 24;

		int startYear = 1970;
		int startMonth = 1;
		int startDayOfMonth = 1;

		int currentYear = startYear;
		int currentMonth = startMonth;
		int currentDayOfMonth = startDayOfMonth;
		int currentHour = (int) (hours % 24) + timeZone;
		int currentMinute = (int) (minutes % 60);
		int currentSecond = (int) (seconds % 60);

		long dayOfYear = days;
		for (int daysOfYear = getDaysOfYear(currentYear); dayOfYear > daysOfYear; ++currentYear, daysOfYear = getDaysOfYear(currentYear)) {
			dayOfYear -= daysOfYear;
		}
		currentDayOfMonth = (int) dayOfYear;
		for (int daysOfMonth = getDaysOfMonth(currentYear, currentMonth); currentDayOfMonth > daysOfMonth; ++currentMonth, daysOfMonth = getDaysOfMonth(
				currentYear, currentMonth)) {
			currentDayOfMonth -= getDaysOfMonth(currentYear, currentMonth);
		}
		++currentDayOfMonth;

		assertEquals(cal.get(Calendar.YEAR), currentYear);
		assertEquals(cal.get(Calendar.MONTH) + 1, currentMonth);
		assertEquals(cal.get(Calendar.DAY_OF_MONTH), currentDayOfMonth);
		assertEquals(cal.get(Calendar.HOUR_OF_DAY), currentHour);
		assertEquals(cal.get(Calendar.MINUTE), currentMinute);
		assertEquals(cal.get(Calendar.SECOND), currentSecond);
	}

	private int getDaysOfYear(int year) {
		return CalendarUtil.getDaysOfYear(year);
	}

	private int getDaysOfMonth(int year, int month) throws IllegalArgumentException {
		return CalendarUtil.getDaysOfMonth(year, month);
	}
	
	@Test
	public void testMilliSecondsInLong() {
		long msPerYear = 1000L * 60L * 60L * 24L * 365L;
		System.out.println(msPerYear);
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MAX_VALUE / msPerYear);
	}
}
